extends Node

var xr_interface: XRInterface

# 运行模式
enum RunningMode {
	NonVR,
	VR
}

var mode: RunningMode = RunningMode.NonVR

func _ready():
	# 检查是否有VR设备
	check_xr_interface()
	
	get_linux_system_info()
	
	# 根据运行模式切换场景
	if mode == RunningMode.VR:
		start_vr_scene()
	else:
		start_nonvr_scene()
		
# 检查是否有VR设备
func check_xr_interface() -> void:
	xr_interface = XRServer.find_interface("OpenXR")
	if xr_interface and xr_interface.is_initialized():
		mode = RunningMode.VR
		print("OpenXR initialized successfully")

		DisplayServer.window_set_vsync_mode(DisplayServer.VSYNC_DISABLED)
		get_viewport().use_xr = true
	else:
		print("OpenXR not initialized, please check if your headset is connected")
		mode = RunningMode.NonVR
		
	
func get_linux_system_info() -> void:
	# 获取发行版信息
	var os_name = execute_command("lsb_release -d")
	var kernel_version = execute_command("uname -r")

	print("OS: ", os_name)
	print("Kernel: ", kernel_version)

func execute_command(command: String) -> Array:
	var output = []
	var args = command.split(" ")
	var process = OS.execute(args[0], args.slice(1), output, false, true)
	if process == 0:
		return output
	else:
		return output


# 加载VR场景
func start_vr_scene():
	var scene = ResourceLoader.load("res://scenes/vr.tscn")
	if scene:
		call_deferred("_change_to_vr_scene", scene)
	else:
		push_error("Failed to load VR scene!")

func _change_to_vr_scene(scene):
	get_tree().change_scene_to_packed(scene)

# 加载NonVR场景
func start_nonvr_scene():
	var scene = ResourceLoader.load("res://scenes/nonvr.tscn")
	if scene:
		call_deferred("_change_to_nonvr_scene", scene)
	else:
		push_error("Failed to load non-VR scene!")

func _change_to_nonvr_scene(scene):
	get_tree().change_scene_to_packed(scene)
